#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtXml\QtXml>
#include <QtXml\QDomDocument>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

#define SEP_FORMMART            "##@@"
#define SEP_FORMMART2            "###@@@"

typedef struct LocationInfo {
    QString filename;
    QString line;
}LocationInfo;

typedef struct TranslateMessage {
    QString name;
    QString source;
    QVector<LocationInfo> locationInfo;
    QString translation;
    TranslateMessage(){
        name = "";
        source = "";
        translation = "";
        locationInfo.clear();
    }

    QString locationToString() {
        QVector<QString>vect;
        QString result = "";
        for(auto idx = 0; idx < locationInfo.size(); idx++) {
            vect << locationInfo[idx].filename + SEP_FORMMART + locationInfo[idx].line;
        }
        for(auto idx = 0; idx < vect.size(); idx++) {
            result += vect[idx];
            if(idx != (vect.size() - 1))
                result += SEP_FORMMART2;
        }

        return result;
    }

    void stringToLcation(const QString& info){
        QVector<QStringRef> vec = info.splitRef(SEP_FORMMART2);
        QVector<QString> vecString;
        for (const QStringRef& ref : vec) {
            vecString.push_back(ref.toString());
        }

        for(auto idx = 0; idx < vecString.size(); idx++) {
            LocationInfo info2;
            info2.filename = vecString[idx].left(vecString[idx].indexOf(SEP_FORMMART));
            info2.line = vecString[idx].mid(vecString[idx].indexOf(SEP_FORMMART)+4);
            locationInfo.push_back(info2);
        }
    }

}TranslateMessage;

typedef enum emDefinedString{
    ts = 0,
    version = 1,
    language = 2,
    context,
    name,
    mac_menu,
    trans_object,
    trans_text,
    message,
    location,
    filename,
    line,
    source,
    translation,
    type,
    vanished
}emDefinedString;

extern QMap<emDefinedString, QString>g_mapDefinedString;
extern QMap<QString, QString>g_mapTsName;

class ParseXmlData : public QObject
{
      Q_OBJECT
public:
    ParseXmlData(QObject *parent = nullptr){}
      ~ParseXmlData(){}

    bool parseTsFile(const QString& ts_file);
    bool writeTsFile(const QString& ts_file);
    //bool writeCsvFile(const QString &csv_file);
    bool writeXlsxFile(const QString& xlsx_file);
    bool readXlsxFile(const QString& xlsx_file);

private:
    QList<QDomNode> findDomElement(QDomNode inElem, const QString& nodeName);

    QString m_version = "2.1";
    QString m_language = "";
    QVector<TranslateMessage> m_vector;

};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

    void on_pushButton_5_clicked();

    void on_pushButton_3_clicked();

    void on_pushButton_6_clicked();

    void on_pushButton_7_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_4_clicked();

private:
    Ui::MainWindow *ui;
    ParseXmlData* m_parseXml = nullptr;

};
#endif // MAINWINDOW_H
